<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class login_controller extends admin_controller
{
    public function index()
    {
        if($this->is_admin_logged()) redirect(base_url(). $this->base_admin_url .'user_controller');
        $data['show_menu'] = false;
        $data['action'] = base_url().'login/check-login';
        $data['theme_path'] = base_url();
        $data['base_admin_url'] = $this->base_admin_url;
        $this->load->view("admin/master_page/header",$data);
        $this->load->view("admin/login",$data);
        $this->load->view("admin/master_page/footer",$data);
    }

    public function check_login()
    {
        $username = $this->input->post('username');
        $password = $this->input->post('password');
        $user = $this->user->get_element_by_username_and_password($username,$password);
        if(isset($user->id))
        {
            $this->set_admin_logged($user);
            redirect(base_url(). $this->base_admin_url . 'user_controller');
        }
        else
        {
            $this->unset_admin_logged();
            $data['message'] = 'Sai tên đăng nhập hoặc mật khẩu.';
            $data['action'] = base_url().'login/check-login';
            $data['theme_path'] = base_url();
            $data['base_admin_url'] = $this->base_admin_url;
            $this->load->view("admin/master_page/header",$data);
            $this->load->view("admin/login",$data);
            $this->load->view("admin/master_page/footer",$data);
        }
    }

    public function can_this_user_access_this_page()
    {
        return true;
    }

    public function logout()
    {
        $this->unset_admin_logged();
        redirect(base_url().'ci-admin');
    }
}